<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaPurchasedVehiclesAmountDetail
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_purchased_vehicles_amount_detail';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition,$adminId)
    {

        $list = Db::table('wa_purchased_vehicles_amount_detail')
            ->select('*')
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();

        if ($list) {
            foreach ($list as $k){
                // 货币类型   1 人民币 2 美元 3 欧元
//                if ($k->currency == 1 ){
//                    $k->currency_name = '人民币';
//                }elseif ($k->currency == 2){
//                    $k->currency_name = '美元';
//                }elseif ($k->currency == 3){
//                    $k->currency_name = '欧元';
//                }else{
//                    $k->currency_name = '';
//                }

                $k->currency_name = IndexController::currencyNameCommon($k->currency);

                // 审批流
                $approve_data = WaPayrollRequisitionExamineApprove::find($adminId,[['purchase_payroll_sn', '=', $k->purchase_payroll_sn],['status','!=',3 ]]);
                $approve_data = json_decode($approve_data);
                if ($approve_data){
                    $approve = WaPayrollRequisitionExamineApproveFlow::all(
                        ['purchase_payroll_sn' => $k->purchase_payroll_sn,'pr_no' => $approve_data->pr_no]);
                    $approve = json_decode($approve);

                    if ($approve) {
                        foreach ($approve as $kpr) {
                            if ($kpr->admin_ids){
                                $kpr->admin_ids = json_decode($kpr->admin_ids);
                            }
                            if ($kpr->status == 3) {
                                $approve_status = 3; // 已驳回
                                $approve_status_name = '已撤回';
                            } elseif ($kpr->status == 2) {
                                $approve_status = 2; // 已驳回
                                $approve_status_name = '已驳回';
                            } elseif ($kpr->status == 1) {
                                if (in_array($adminId->id, $kpr->admin_ids)){
                                    $approve_status = 1; // 已审批
                                    $approve_status_name = '已审批';
                                }else{
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } elseif ($kpr->status == 0) {
                                if (in_array($adminId->id, $kpr->admin_ids)){
                                    $approve_status = 0; // 待审批
                                    $approve_status_name = '待审批';
                                }else{
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } else {
                                $approve_status = 999; // 已驳回
                                $approve_status_name = '不在审批流中';
                            }
                        }
                    } else {
                        $approve_status = 10001; // 审批状态
                        $approve_status_name = '没有审批记录';// 审批状态名称
                    }


                }else{
                    $approve_status = 10001;// 待提交审批
                    $approve_status_name = '待提交审批/或不是审批人员';
                }

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称



                $k->un_settlement =  WaPurchasedVehiclesAmountDetail::sumsByUnSettlementByPriceName($k->vin,$k->id); // 未付款
                $k->already_settlement =  WaPurchasedVehiclesAmountDetail::sumsByAlreadySettlementByPriceName($k->vin,$k->id); // 已请款

                $k->month_bill_status = ''; // 结算状态 1 未结清 2 结款中 3 已结清 4 撤回
                // 支付方式  1 现结 2 月结
                if ($k->payment_type == 1){
                    $k->payment_type_name = '现结付款';
                    // 付款类型  0 待请款 1 审批中 2 未付款 3 已付款 4 审核不通过 5 撤回 6 删除
                    if ($k->payment_status == 0){
                        $k->payment_status_name = '待请款';
                    }elseif ($k->payment_status == 1){
                        $k->payment_status_name = '审批中';
                    }elseif ($k->payment_status == 2){
                        $k->payment_status_name = '未付款';
                    }elseif ($k->payment_status == 3){
                        $k->payment_status_name = '已付款';
                    }elseif ($k->payment_status == 4){
                        $k->payment_status_name = '审核不通过';
                    }elseif ($k->payment_status == 5){
                        $k->payment_status_name = '撤回';
                    }elseif ($k->payment_status == 6){
                        $k->payment_status_name = '删除';
                    }else{
                        $k->payment_status_name = '暂无该款项状态';
                    }




                }elseif($k->payment_type == 2){
                    $k->payment_type_name = '月结付款';
                    $condition_month = [];
                    $condition_month [] = ['vin','=',$k->vin];
                    $wms = WaMonthlySettlement::find($condition_month);
                    $wms= json_decode($wms);
                    if ($wms){
                        if ($wms->settlement_sratus){
                            // 月结账单状态   1 未结清 2 结款中 3 已结清
                            $k->month_bill_status = $wms->settlement_sratus;
//                            if($wms->settlement_sratus == 1){
//                                $k->month_bill_status_name = '未结清';
//                            }elseif ($wms->settlement_sratus == 2){
//                                $k->month_bill_status_name = '结款中';
//                            }elseif ($wms->settlement_sratus == 3){
//                                $k->month_bill_status_name = '已结清';
//                            }
                            $k->month_bill_status_name = IndexController::billStatusName($wms->settlement_sratus);

                        }
                    }

                    // 请款状态 //存在多次请款（超额）但是产品没有规划 所有不限制 款项重复添加问题
                    if ($wms->payroll_approve_sn){
                        $condition_month_approve = [];
                        $condition_month_approve [] = ['pr_no','=',$wms->payroll_approve_sn];
                        $month_approve_status = WaPayrollRequisitionExamineApprove::findApproveStatusByPrNo($condition_month_approve);
                        $month_approve_status= json_decode($month_approve_status);
                        // 审批状态 0 审批中 1 通过 2 驳回 3 撤回
                        if ($month_approve_status->status == 0){
                            $k->month_approve_stauts_name = '审批中';
                        }elseif ($month_approve_status->status == 1){
                            $k->month_approve_stauts_name = '审批通过';
                        }elseif ($month_approve_status->status == 2){
                            $k->month_approve_stauts_name = '审批驳回';
                        }elseif ($month_approve_status->status == 3){
                            $k->month_approve_stauts_name = '审批撤回';
                        }else{
                            $k->month_approve_stauts_name = '暂无该审批状态';
                        }
                    }


                }else{
                    $k->payment_type_name = '';
                }

            }

        }
        return json_encode($list);

    }

    public static function findByPaymentStatusBetween($condition,$between)
    {

        if ($between){
            $list = Db::table('wa_purchased_vehicles_amount_detail')
                ->select('*')
                ->where($condition)
                ->whereBetween('payment_status',$between)
                ->orderBy('id', 'asc')
                ->get();

        }else{
            $list = Db::table('wa_purchased_vehicles_amount_detail')
                ->select('*')
                ->where($condition)
                ->orderBy('id', 'asc')
                ->get();

        }

        if ($list) {
            foreach ($list as $k){
                $k->un_settlement =  WaPurchasedVehiclesAmountDetail::sumsByUnSettlementByPriceName($k->vin,$k->id); // 未付款
                $k->already_settlement =  WaPurchasedVehiclesAmountDetail::sumsByAlreadySettlementByPriceName($k->vin,$k->id); // 已请款
//                // 月结账单状态   1 未结清 2 结款中 3 已结清
//                if ($k->month_bill_status == 1){
//                    $k->month_bill_status_name = '未结清';
//                }elseif($k->month_bill_status == 2){
//                    $k->month_bill_status_name = '结款中';
//                }elseif($k->month_bill_status == 3){
//                    $k->month_bill_status_name = '已结清';
//                }else{
//                    $k->month_bill_status_name = '';
//                }

                $k->month_bill_status = ''; // 结算状态 1 未结清 2 结款中 3 已结清 4 撤回
                // 支付方式  1 现结 2 月结
                if ($k->payment_type == 1){
                    $k->payment_type_name = '现结付款';
                    // 付款类型  0 待请款 1 审批中 2 未付款 3 已付款 4 审核不通过 5 撤回 6 删除
                    if ($k->payment_status == 0){
                        $k->payment_status_name = '待请款';
                    }elseif ($k->payment_status == 1){
                        $k->payment_status_name = '审批中';
                    }elseif ($k->payment_status == 2){
                        $k->payment_status_name = '未付款';
                    }elseif ($k->payment_status == 3){
                        $k->payment_status_name = '已付款';
                    }elseif ($k->payment_status == 4){
                        $k->payment_status_name = '审核不通过';
                    }elseif ($k->payment_status == 5){
                        $k->payment_status_name = '撤回';
                    }elseif ($k->payment_status == 6){
                        $k->payment_status_name = '删除';
                    }else{
                        $k->payment_status_name = '暂无该款项状态';
                    }




                }elseif($k->payment_type == 2){
                    $k->payment_type_name = '月结付款';
                    $condition_month = [];
                    $condition_month [] = ['vin','=',$k->vin];
                    $wms = WaMonthlySettlement::find($condition_month);
                    $wms= json_decode($wms);
                    if ($wms){
                        if ($wms->settlement_sratus){
                            // 月结账单状态   1 未结清 2 结款中 3 已结清
                            $k->month_bill_status = $wms->settlement_sratus;
//                            if($wms->settlement_sratus == 1){
//                                $k->month_bill_status_name = '未结清';
//                            }elseif ($wms->settlement_sratus == 2){
//                                $k->month_bill_status_name = '结款中';
//                            }elseif ($wms->settlement_sratus == 3){
//                                $k->month_bill_status_name = '已结清';
//                            }
                            $k->month_bill_status_name = IndexController::billStatusName($wms->settlement_sratus);

                        }
                    }

                    // 请款状态 //存在多次请款（超额）但是产品没有规划 所有不限制 款项重复添加问题
                    if ($wms->payroll_approve_sn){
                        $condition_month_approve = [];
                        $condition_month_approve [] = ['pr_no','=',$wms->payroll_approve_sn];
                        $month_approve_status = WaPayrollRequisitionExamineApprove::findApproveStatusByPrNo($condition_month_approve);
                        $month_approve_status= json_decode($month_approve_status);
                        // 审批状态 0 审批中 1 通过 2 驳回 3 撤回
                        if ($month_approve_status->status == 0){
                            $k->month_approve_stauts_name = '审批中';
                        }elseif ($month_approve_status->status == 1){
                            $k->month_approve_stauts_name = '审批通过';
                        }elseif ($month_approve_status->status == 2){
                            $k->month_approve_stauts_name = '审批驳回';
                        }elseif ($month_approve_status->status == 3){
                            $k->month_approve_stauts_name = '审批撤回';
                        }else{
                            $k->month_approve_stauts_name = '暂无该审批状态';
                        }
                    }


                }else{
                    $k->payment_type_name = '';
                }

            }

        }
        return json_encode($list);

    }


    public static function created($data)
    {
        $result = Db::table('wa_purchased_vehicles_amount_detail')->insertGetId($data);
        return $result;
    }

    public static function updated($data, $where)
    {
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->update($data);
        return $result;
    }

    public static function deleted($where)
    {
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->delete();
        return $result;
    }

    public static function list($page, $pageNum,$fields = ['*'], $condition = [])
    {

        $offset = ($page - 1) * $pageNum;

            $model = Db::table('wa_purchased_vehicles_amount_detail')
                ->select($fields)
                ->where($condition);



        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);

        if ($list) {
            foreach ($list as $k){
                $k->un_settlement =  WaPurchasedVehiclesAmountDetail::sumsByUnSettlementByPriceName($k->vin,$k->id); // 未付款
                $k->already_settlement =  WaPurchasedVehiclesAmountDetail::sumsByAlreadySettlementByPriceName($k->vin,$k->id); // 已请款
            }

        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    public static function find($condition)
    {
        $k = Db::table('wa_purchased_vehicles_amount_detail')
            ->where($condition)
            ->first();
        if($k){
            // 支付方式  1 现结 2 月结
            if ($k->payment_type == 1){
                $k->payment_type_name = '现结付款';
                // 付款类型  0 待请款 1 审批中 2 未付款 3 已付款 4 审核不通过 5 撤回 6 删除
                if ($k->payment_status == 0){
                    $k->payment_status_name = '待请款';
                }elseif ($k->payment_status == 1){
                    $k->payment_status_name = '审批中';
                }elseif ($k->payment_status == 2){
                    $k->payment_status_name = '未付款';
                }elseif ($k->payment_status == 3){
                    $k->payment_status_name = '已付款';
                }elseif ($k->payment_status == 4){
                    $k->payment_status_name = '审核不通过';
                }elseif ($k->payment_status == 5){
                    $k->payment_status_name = '撤回';
                }elseif ($k->payment_status == 6){
                    $k->payment_status_name = '删除';
                }else{
                    $k->payment_status_name = '暂无该款项状态';
                }




            }elseif($k->payment_type == 2){
                $k->payment_type_name = '月结付款';
                $condition_month = [];
                $condition_month [] = ['vin','=',$k->vin];
                $wms = WaMonthlySettlement::find($condition_month);
                $wms= json_decode($wms);
                if ($wms){
                    if ($wms->settlement_sratus){
                        // 月结账单状态   1 未结清 2 结款中 3 已结清
                        $k->month_bill_status = $wms->settlement_sratus;
//                        if($wms->settlement_sratus == 1){
//                            $k->month_bill_status_name = '未结清';
//                        }elseif ($wms->settlement_sratus == 2){
//                            $k->month_bill_status_name = '结款中';
//                        }elseif ($wms->settlement_sratus == 3){
//                            $k->month_bill_status_name = '已结清';
//                        }
                        $k->month_bill_status_name = IndexController::billStatusName($wms->settlement_sratus);

                    }
                }

                // 请款状态 //存在多次请款（超额）但是产品没有规划 所有不限制 款项重复添加问题
                if ($wms->payroll_approve_sn){
                    $condition_month_approve = [];
                    $condition_month_approve [] = ['pr_no','=',$wms->payroll_approve_sn];
                    $month_approve_status = WaPayrollRequisitionExamineApprove::findApproveStatusByPrNo($condition_month_approve);
                    $month_approve_status= json_decode($month_approve_status);
                    // 审批状态 0 审批中 1 通过 2 驳回 3 撤回
                    if ($month_approve_status->status == 0){
                        $k->month_approve_stauts_name = '审批中';
                    }elseif ($month_approve_status->status == 1){
                        $k->month_approve_stauts_name = '审批通过';
                    }elseif ($month_approve_status->status == 2){
                        $k->month_approve_stauts_name = '审批驳回';
                    }elseif ($month_approve_status->status == 3){
                        $k->month_approve_stauts_name = '审批撤回';
                    }else{
                        $k->month_approve_stauts_name = '暂无该审批状态';
                    }
                }


            }else{
                $k->payment_type_name = '';
            }
            // 支付方式  1 现结 2 月结
            if ($k->payment_type == 1){
                $k->payment_type_name = '现结付款';
            }elseif($k->payment_type == 2){
                $k->payment_type_name = '月结付款';
            }else{
                $k->payment_type_name = '';
            }


            // 创建类型 1 自动 2 手动
            if ($k->created_type == 1){
                $k->created_type_name = '自动';
            }elseif($k->created_type == 2){
                $k->created_type_name = '手动';
            }else{
                $k->created_type_name = '暂无';
            }

            }
        return json_encode($k);
    }

    // 费用合计
    public static function sumsByTotalPrice($where){
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('total_price');
        return $result;
    }
    // 已请款总额
    public static function sumsByRequestedPrice($where){
        $where [] = ['payment_status', '>', 1];
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('requested_price');
        return $result;
    }
    // 未结算总额
    public static function sumsByUnSettlement($where){
        $where [] = ['payment_status','=',2];
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('this_request_price');
        return $result;
    }
    // 已结算总额
    public static function sumsByAlreadySettlement($where){
        $where [] = ['payment_status','=',3];
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('this_request_price');
        return $result;
    }

    // 未付款
    public static function sumsByUnSettlementByPriceName($vin,$id){
        $where = [];
        $where [] = ['vin','=',$vin];
        $where [] = ['payment_status','=',2];
        $where [] = ['id','=',$id];
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('this_request_price');
        return $result;
    }
    // 已付款
    public static function sumsByAlreadySettlementByPriceName($vin,$id){
        $where = [];
        $where [] = ['vin','=',$vin];
        $where [] = ['payment_status','=',3];
        $where [] = ['id','=',$id];
        $result = Db::table('wa_purchased_vehicles_amount_detail')->where($where)->sum('this_request_price');
        return $result;
    }

}